package factorytest.iflytek.com.einkiflytekfactorytest.engineerui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;


import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.ViewDialogFragment;
import factorytest.iflytek.com.einkiflytekfactorytest.utils.DisplayUtil;


/**
 * Created by sst on 2017/12/30.
 */
public class PWActivity extends Activity {

    private Context mContext;
    private PopupWindow popupWindowShape;
    private PopupWindow popupWindowBackground;
    private PopupWindow popupWindowLineWidth;

    private ImageView imgPWShape;
    private ImageView imgPWBackground;
    private ImageView imgPWLineWidth;
    private ImageView imgPWUndo;

    private ImageView surfaceView_bg_ImageView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.pw_activity);

        imgPWShape = (ImageView) findViewById(R.id.imgPWShape);
        imgPWBackground = (ImageView) findViewById(R.id.imgPWBackground);
        imgPWLineWidth = (ImageView) findViewById(R.id.imgPWLineWidth);
        imgPWUndo = (ImageView) findViewById(R.id.imgPWUndo);
        surfaceView_bg_ImageView = (ImageView) findViewById(R.id.surfaceView_bg_ImageView);

        mContext = this;

        initShape();
        initBackground();
        initLineWidth();

        imgPWShape.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(popupWindowShape);
                popupWindowShape.showAsDropDown(imgPWShape, 0, 0);
            }
        });

        imgPWBackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(popupWindowBackground);
                popupWindowBackground.showAsDropDown(imgPWBackground, 0, 0);
            }
        });

        imgPWLineWidth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(popupWindowLineWidth);
                popupWindowLineWidth.showAsDropDown(imgPWLineWidth, 0, 0);
            }
        });

        imgPWUndo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        findViewById(R.id.btnFinish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showFinishDialog(R.string.pw_test, R.string.item_exit_test);

            }
        });

    }

    private void showFinishDialog(int titleRes, int msgRes) {
        final String title = getString(titleRes);
        final String msg = getString(msgRes);
        ViewDialogFragment dialog = ViewDialogFragment.newInstance(title, msg);

        dialog.show(getFragmentManager(),
                new ViewDialogFragment.Callback() {
                    @Override
                    public void onClick() {

                        writeResultIntoDB(getResources().getString(R.string.test_ok));

                        Toast.makeText(mContext, R.string.test_ok, Toast.LENGTH_SHORT).show();
                        setResult(RESULT_OK);
                        ActivityCompat.finishAfterTransition(PWActivity.this);

                    }
                },
                new ViewDialogFragment.Callback() {
                    @Override
                    public void onClick() {

                        writeResultIntoDB(getResources().getString(R.string.test_fail));

                        Toast.makeText(mContext, R.string.test_fail, Toast.LENGTH_SHORT).show();
                        setResult(RESULT_CANCELED);
                        ActivityCompat.finishAfterTransition(PWActivity.this);
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();

        surfaceView_bg_ImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);


        updatePWDrawSurfaceView();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    private void updatePWDrawSurfaceView() {


    }


    private PopupWindow initPopupWindow(Context context, @LayoutRes int layoutRes) {

        PopupWindow pw = new PopupWindow(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity
                .LAYOUT_INFLATER_SERVICE);

        pw.setContentView(inflater.inflate(layoutRes, null));//设置主体布局
        pw.setFocusable(true);
        pw.setAnimationStyle(0);

        pw.setWidth(DisplayUtil.getScreenWidth(context));//宽度200dp
        int height = getResources().getDimensionPixelSize(R.dimen.activity_pw_func_main_height);
        pw.setHeight(height + 10);
        return pw;
    }

    private void initLineWidth() {
        popupWindowLineWidth = initPopupWindow(this, R.layout.htfyun_popup_pw_pen_width);
        View view = popupWindowLineWidth.getContentView();

//        final View width0 = view.findViewById(R.id.einkdraw_pen_width_0);
//        width0.setTag(OwnerSurfaceView.MIN_PEN_WIDTH_PIXEL);
//        width0.setOnClickListener(clickListenerLineWidth);
//
//        final View width1 = view.findViewById(R.id.einkdraw_pen_width_1);
//        width1.setTag(OwnerSurfaceView.MIN_PEN_WIDTH_PIXEL + 1);
//        width1.setOnClickListener(clickListenerLineWidth);
//
//        final View width2 = view.findViewById(R.id.einkdraw_pen_width_2);
//        width2.setTag(OwnerSurfaceView.MIN_PEN_WIDTH_PIXEL + 2);
//        width2.setOnClickListener(clickListenerLineWidth);
//
//        final View width3 = view.findViewById(R.id.einkdraw_pen_width_3);
//        width3.setTag(OwnerSurfaceView.MIN_PEN_WIDTH_PIXEL + 3);
//        width3.setOnClickListener(clickListenerLineWidth);
//
//        final View width4 = view.findViewById(R.id.einkdraw_pen_width_4);
//        width4.setTag(OwnerSurfaceView.MIN_PEN_WIDTH_PIXEL + 4);
//        width4.setOnClickListener(clickListenerLineWidth);
    }

    private View.OnClickListener clickListenerLineWidth = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            pwdraw_surface_view.setPenSettingWidth((int)v.getTag());
            popupWindowLineWidth.dismiss();
        }
    };

    private void initBackground() {
        popupWindowBackground = initPopupWindow(this, R.layout.htfyun_popup_pw_background);

//        View view = popupWindowBackground.getContentView();
//
//        final View bg_none = view.findViewById(R.id.einkdraw_bg_none);
//        bg_none.setTag(OwnerSurfaceView.WT_TYPE_NONE);
//        bg_none.setOnClickListener(clickListenerBackground);
//
//        final View bg_intersected = view.findViewById(R.id.einkdraw_bg_intersected);
//        bg_intersected.setTag(OwnerSurfaceView.WT_TYPE_INTERSECTED);
//        bg_intersected.setOnClickListener(clickListenerBackground);
//
//        final View bg_linefigure = view.findViewById(R.id.einkdraw_bg_linefigure);
//        bg_linefigure.setTag(OwnerSurfaceView.WT_TYPE_LINEFIGURE);
//        bg_linefigure.setOnClickListener(clickListenerBackground);
//
//        final View bg_matts = view.findViewById(R.id.einkdraw_bg_matts);
//        bg_matts.setTag(OwnerSurfaceView.WT_TYPE_MATTS);
//        bg_matts.setOnClickListener(clickListenerBackground);

    }


    private View.OnClickListener clickListenerBackground = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            int wtType = (int) v.getTag();
//            int dpSize = 0;//OwnerSurfaceView.WM_DEFAULT_UNIT_SIZE_DP;
//            if (wtType == OwnerSurfaceView.WT_TYPE_LINEFIGURE) {
//                dpSize = 80;
//            } else if (wtType == OwnerSurfaceView.WT_TYPE_MATTS) {
//                dpSize = 100;
//            } else if (wtType == OwnerSurfaceView.WT_TYPE_INTERSECTED) {
//                dpSize = 120;
//            }
//            pwdraw_surface_view.setBackGroupMatts((int)v.getTag(), dpSize);
//            popupWindowBackground.dismiss();
        }
    };

    private void initShape() {
        popupWindowShape = initPopupWindow(this, R.layout.htfyun_popup_pw_shape);

//        View view = popupWindowShape.getContentView();
//
//        final View obj_pw = view.findViewById(R.id.einkdraw_obj_pw);
//        obj_pw.setTag(OwnerSurfaceView.DRAW_OBJ_RANDOM);
//
//        obj_pw.setOnClickListener(clickListenerShape);
//
//        final View obj_line = view.findViewById(R.id.einkdraw_obj_line);
//        obj_line.setTag(OwnerSurfaceView.DRAW_OBJ_LINE);
//        obj_line.setOnClickListener(clickListenerShape);
//
//        final View obj_circle = view.findViewById(R.id.einkdraw_obj_circle);
//        obj_circle.setTag(OwnerSurfaceView.DRAW_OBJ_CIRCLE);
//        obj_circle.setOnClickListener(clickListenerShape);
//
//        final View obj_rect = view.findViewById(R.id.einkdraw_obj_rect);
//        obj_rect.setTag(OwnerSurfaceView.DRAW_OBJ_RECTANGLE);
//        obj_rect.setOnClickListener(clickListenerShape);
//
//        final View obj_triangle = view.findViewById(R.id.einkdraw_obj_triangle);
//        obj_triangle.setTag(OwnerSurfaceView.DRAW_OBJ_TRIANGLE);
//        obj_triangle.setOnClickListener(clickListenerShape);

    }

    private View.OnClickListener clickListenerShape = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            pwdraw_surface_view.setDrawObjectType((int)v.getTag());
            popupWindowShape.dismiss();
        }
    };

    private void showPopupWindow(PopupWindow popupWindow) {


        int objType = -1;//pwdraw_surface_view.getDrawObjectType();

//        if (popupWindow == popupWindowShape) {
//            objType = pwdraw_surface_view.getDrawObjectType();
//        } else if (popupWindow == popupWindowBackground) {
//            objType = pwdraw_surface_view.getBackGroundMattsType();
//        } else if (popupWindow == popupWindowLineWidth) {
//            objType = pwdraw_surface_view.getPenSettingWidth();
//        } else {
//
//            return;
//        }

        ViewGroup viewGroup = (ViewGroup)popupWindow.getContentView();

        for (int i = 0; i < viewGroup.getChildCount(); i ++) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof ViewGroup) {
                ViewGroup childGroup = (ViewGroup) view;
                for (int j = 0; j < childGroup.getChildCount(); j++) {
                    View childView = childGroup.getChildAt(j);
                    if (!(childView instanceof ImageView)
                            && view.getTag() != null
                            && (view.getTag() instanceof  Integer)) {
                        childView.setVisibility((objType == (int) view.getTag()) ? View.VISIBLE : View.INVISIBLE);
                    }
                }
            }
        }

    }

    private void writeResultIntoDB(String result) {

        //ItemDBOperation.updateResultIntoDB(this, TestItemAttr.penWrite, result);

    }

}
